local hl__taoluan = fk.CreateSkill {
  name = "hl__taoluan",
}

Fk:loadTranslationTable{
  ["hl__taoluan"] = "滔乱",
  [":hl__taoluan"] = "每种牌名限一次、每回合每种花色限一次，你可以将一张牌当任意基本牌或普通锦囊牌使用，"..
  "然后令一名其他角色选择：1.交给你一张类别不同的牌；2.本回合此技能失效，且此回合结束时其失去1点体力。",

  ["#hl__taoluan"] = "滔乱：将一张牌当任意基本牌或普通锦囊牌使用",
  ["@[hl__taoluan]"] = "滔乱",
  ["#hl__taoluan-choose"] = "滔乱：选择一名角色，其交给你一张非%arg或令你失去1点体力且本回合“滔乱”失效",
  ["#hl__taoluan-card"] = "滔乱：交给 %src 一张非%arg，否则你失去1点体力且本回合“滔乱”失效",

  ["$hl__taoluan1"] = "大火方取栗，浑水好摸鱼！",
  ["$hl__taoluan2"] = "横眉冷对江河覆，搅翻日月乱人间！",
}

Fk:addQmlMark{
  name = "hl__taoluan",
  how_to_show = function(_, value)
    if type(value) ~= "table" then return " " end
    if value.loseHp then return Fk:translate("lose_hp") end
    if type(value.suits) ~= "table" or #value.suits == 0 then return " " end
    return table.concat(table.map(value.suits, function(s)
      return Fk:translate(s)
    end), "")
  end,
  qml_data = function(name, value, p)
    if type(value) ~= "table" then return value end
    return value.value
  end,
  qml_path = "packages/utility/qml/ViewPile"
}

hl__taoluan:addEffect("viewas", {
  pattern = ".",
  prompt = "#hl__taoluan",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("bt")
    local names = player:getViewAsCardNames(hl__taoluan.name, all_names, nil, player:getTableMark("@[hl__taoluan]").value)
    if #names == 0 then return end
    return UI.CardNameBox {choices = names, all_choices = all_names}
  end,
  filter_pattern = function (self, player, card_name, selected)
    local suits = {"spade", "heart", "club", "diamond"}
    local mark = player:getMark("@[hl__taoluan]")
    if type(mark) == "table" then
      for _, v in ipairs(mark.suits) do
        table.removeOne(suits, string.sub(v, 5))
      end
    end
    if #suits > 0 then
      return {
        min_num = 1,
        max_num = 1,
        pattern = ".|.|".. table.concat(suits, ","),
      }
    end
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 or Fk.all_card_types[self.interaction.data] == nil then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcard(cards[1])
    card.skillName = hl__taoluan.name
    return card
  end,
  before_use = function(self, player, use)
    local mark = player:getMark("@[hl__taoluan]")
    if type(mark) ~= "table" then
      mark = {
        value = {},
        suits = {},
        loseHp = false,
      }
    end
    table.insert(mark.value, use.card.trueName)
    table.insert(mark.suits, use.card:getSuitString(true))
    player.room:setPlayerMark(player, "@[hl__taoluan]", mark)
  end,
  after_use = function(self, player, use)
    local room = player.room
    if player.dead or #room:getOtherPlayers(player, false) == 0 then return end
    local type = use.card:getTypeString()
    local to = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player, false),
      min_num = 1,
      max_num = 1,
      prompt = "#hl__taoluan-choose:::"..type,
      skill_name = hl__taoluan.name,
      cancelable = false,
    })[1]
    local card = room:askToCards(to, {
      skill_name = hl__taoluan.name,
      min_num = 1,
      max_num = 1,
      pattern = ".|.|.|.|.|^"..type,
      prompt = "#hl__taoluan-card:"..player.id.."::"..type,
    })
    if #card > 0 then
      room:obtainCard(player, card, false, fk.ReasonGive, to.id, hl__taoluan.name)
    elseif player:hasSkill(hl__taoluan.name, true) then
      room:invalidateSkill(player, hl__taoluan.name, "-turn")
      local mark = player:getTableMark("@[hl__taoluan]")
      mark.loseHp = true
      room:setPlayerMark(to, "@[hl__taoluan]", mark)
    end
  end,
  enabled_at_play = function(self, player)
    local mark = player:getMark("@[hl__taoluan]")
    return type(mark) ~= "table" or #mark.suits < 4
  end,
  enabled_at_response = function(self, player, response)
    if response then return end
    local mark = player:getMark("@[hl__taoluan]")
    return type(mark) ~= "table" or
      (#mark.suits < 4 and #player:getViewAsCardNames(hl__taoluan.name, Fk:getAllCardNames("bt"), nil, mark.value) > 0)
  end,
  enabled_at_nullification = function (self, player, data)
    return #player:getHandlyIds() > 0 and self:enabledAtResponse(player, false)
  end
})

hl__taoluan:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@[hl__taoluan]", 0)
end)

hl__taoluan:addEffect(fk.TurnEnd, {
  anim_type = "negative",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local mark = player:getMark("@[hl__taoluan]")
    return type(mark) == "table" and mark.loseHp
  end,
  on_use = function(self, event, target, player, data)
    player.room:loseHp(player, 1, hl__taoluan.name)
    if not player:hasSkill(hl__taoluan.name, true) then
    player.room:setPlayerMark(player, "@[hl__taoluan]", 0)
    end
  end,

  late_refresh = true,
  can_refresh = function(self, event, target, player, data)
    return player:getMark("@[hl__taoluan]") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    local mark = player:getMark("@[hl__taoluan]")
    mark.suits = {}
    mark.loseHp = false
    player.room:setPlayerMark(player, "@[hl__taoluan]", mark)
  end,
})

return hl__taoluan
